package com.txtsqlclient.domain;

import java.util.ArrayList;
import java.util.List;

public class ReadQueryResult extends QueryResult {

	final protected List<List<String>> data;

	final protected int numOfRowsReturned;

	final protected int numOfColumns;

	final protected List<String> header;

	public ReadQueryResult(final List<String> header,
			final List<List<String>> data) {
		// If header is null
		if (header == null) {
			throw new IllegalArgumentException("Header cannot be null");
		}
		// If data is present, verify that the length of each row matches that
		// of the header.
		if (data != null && !data.isEmpty()) {
			int i = 0;
			for (List<String> row : data) {
				if (row.size() != header.size()) {
					throw new IllegalArgumentException(
							"The number of cells in row#" + i + " ("
									+ row.size()
									+ "(does not match that of the header ("
									+ header.size());
				}
				i++;
			}
		}
		this.header = header;
		this.data = data;
		this.numOfRowsReturned = (data != null) ? data.size() : 0;
		this.numOfColumns = header.size();
	}

	public List<List<String>> getData() {
		return this.data;
	}

	public int getNumOfRowsReturned() {
		return this.numOfRowsReturned;
	}

	public int getNumOfColumns() {
		return this.numOfColumns;
	}

	public List<String> getHeader() {
		return this.header;
	}

	public List<List<String>> getHeadersAndData() {
		List<List<String>> result = new ArrayList<List<String>>();
		result.add(this.header);
		if (this.data != null) {
			result.addAll(this.data);
		}
		return result;
	}
}